#!/usr/bin/perl

use Asterisk::Manager;

my $astman = new Asterisk::Manager;

open(FILE, "/etc/amportal.conf") || die "Failed to open amportal.conf\n";
while (<FILE>) {
    chomp;                  # no newline
    s/#.*//;                # no comments
    s/^\s+//;               # no leading white
    s/\s+$//;               # no trailing white
    next unless length;     # anything left?
    my ($var, $value) = split(/\s*=\s*/, $_, 2);
    $User_Preferences{$var} = $value;
}
close(FILE);

$login = $User_Preferences{"AMPMGRUSER"};
$password = $User_Preferences{"AMPMGRPASS"};
$server = $User_Preferences{"MANAGERHOSTS"};

$astman->user($login);
$astman->secret($password);
$astman->host($server);

$astman->connect || die $astman->error . "\n";

$astman->setcallback('Hangup', \&hangup_callback);
$astman->setcallback('DEFAULT', \&default_callback);

print STDERR $astman->command('reload');

$astman->disconnect;

sub hangup_callback {
    print STDERR "hangup callback\n";
}

sub default_callback {
    my (%stuff) = @_;
    foreach (keys %stuff) {
        print STDERR "$_: ". $stuff{$_} . "\n";
    }
    print STDERR "\n";
}
